//
//  AppDelegate.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/9/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 1. 初始化 window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 2. 设置根控制器
//        window?.rootViewController = LXLTabBarController()
        
        
        //判断是否要显示新特性界面
        
        if hasNewVersion(){
            //新特性界面
            window?.rootViewController = LXLNewFeatureViewController()
        }else{
            
//            //判断是否登录
//            let account = LXLUserAccount.loadUserAccount()
//            if let _ = account{
//                //如果登录---->进入到欢迎界面
//                window?.rootViewController = LXLWelcomeViewController()
//            }else{
//                //如果没有登录--->进入到登录界面
//                window?.rootViewController = LXLNavigationController(rootViewController: LXLOAuthViewController())
//            }
            

            //判断是否登录
            //如果登录进入到欢迎界面,
            // 没有登录进入到登录界面
           LXLUserAccount.isLogin(loginSuccess: LXLWelcomeViewController(), noLogin: LXLOAuthViewController())
            
            
            
        }
        //认证页面
//        window?.rootViewController = LXLNavigationController(rootViewController: LXLOAuthViewController())
        
        //欢迎界面
//        window?.rootViewController = LXLWelcomeViewController()
        
        // 3. 让窗口可见，并成为主控器
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    
    // 判断是否有新版本或者是否是第一次使用
    private func hasNewVersion() -> Bool {
        
        
        //取出app的版本号
        let appVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]! as! String
        // 取出本地保存的版本号
        let localVersion = NSUserDefaults.standardUserDefaults().stringForKey("Version")
        
        
        //保存一下当前的版本号
        NSUserDefaults.standardUserDefaults().setObject(appVersion, forKey: "Version")
        NSUserDefaults.standardUserDefaults().synchronize()
        
        if let localVer = localVersion {
            // 升序
            // 代表本地的版本号小于 app 的版本号,返回 true
            return localVer.compare(appVersion) == NSComparisonResult.OrderedAscending
        }
        
        return true
    }
    
    
    
    
    
    
    
    
    
    
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

